package com.wythe.mall.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.wythe.mall.R;
import com.wythe.mall.adapter.ImageShopAdapter;
import com.wythe.mall.intf.OnRequestDataListener;
import com.wythe.mall.model.ShopBean;
import com.wythe.mall.utils.ApiOkHttp;
import com.wythe.mall.view.NoScrollGridView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class HomeShopFragment extends Fragment {
    private View view;
    private NoScrollGridView mGridView;
    private ImageShopAdapter mShopAdapter;
    private List<ShopBean> list;
    private int mId;

    public static HomeShopFragment getInstance(int id) {
        HomeShopFragment fragment = new HomeShopFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("id", id);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mId = bundle.getInt("id");
        }
        if (list == null) {
            list = new ArrayList<>();
        }
        list.clear();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_home_shop, container, false);
        initView();
        getShopList(mId);
        return view;
    }

    private void initView() {
        mGridView = (NoScrollGridView) view.findViewById(R.id.grid_image);
        mShopAdapter = new ImageShopAdapter(getActivity(), list);
        mGridView.setAdapter(mShopAdapter);
    }

    public void getShopList(int type) {
        JSONObject params = new JSONObject();
        try {
            params.put("indexLabelId", type);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiOkHttp.getShopList(getActivity(), params, new OnRequestDataListener() {
            @Override
            public void requestSuccess(int code, JSONObject data) {
                if (list == null) {
                    list = new ArrayList<>();
                }
                list.clear();
                try {
                    JSONArray array = data.getJSONArray("data");
                    for (int i = 0; i < array.length(); i++) {
                        ShopBean bean = new Gson().fromJson(array.getJSONObject(i).toString(), ShopBean.class);
                        list.add(bean);
                    }
                    mShopAdapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void requestFailure(int code, String msg) {

            }
        });

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

}
